package com.yf.words.service.impl;

import com.yf.words.excel.ExcelReaderUtil;
import com.yf.words.excel.Tags;
import com.yf.words.service.TagService;
import com.yf.words.util.DataUtil;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TagServiceImpl implements TagService {

    private List<Tags> tags;

    private Map<String, Tags> tagsMap;

    @PostConstruct
    private void init() {
        tags = DataUtil.getListData(Tags.class, ExcelReaderUtil.FILE_WORDS, "tags").stream().filter(i -> i.getWord() != null).toList();

        tagsMap = tags.stream().collect(Collectors.toMap(Tags::getWord, tag -> tag));
    }

    @Override
    public List<String> getTagsByWord(String word) {
        Tags tags = tagsMap.get(word);
        if (tags == null) {
            return null;
        }
        return List.of(tags.getTags().split(";"));
    }
}